package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @Author liangzai
 * @Description:
 */

class MyThreadPools{
    private BlockingQueue<Runnable> queue = null;

    public MyThreadPools(int n){
        queue = new ArrayBlockingQueue<>(1000);

        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                while (true) {
                    try {
                        queue.take().run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPools threadPools = new MyThreadPools(4);
        for (int i = 0; i <= 100; i++) {
            int id = i;
            threadPools.submit(()->{
                System.out.println(Thread.currentThread().getName()+" "+id);
            });
        }
    }
}
